/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.product;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hutech09.jsale.bsn.ICategoryManager;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Category;
import org.hutech09.jsale.entity.Product;
import org.hutech09.jsale.entity.ProductType;

/**
 *
 * @author Nam
 */
@ManagedBean
@SessionScoped
public class CategoryList {

    @EJB
    private ICategoryManager cm;
    @EJB
    private IProductManager pm;

    private List<Category> categories;

    private List<ProductType> types;

    private Product product;

    private Category category;

    private ProductType type;

    public Category getCategory() {
        return category;
    }

    public void setSelectedProduct(Product p) {
        reset();
        this.product = p;
        this.type = p.getProductType();
    }
    
    public void updateProduct(){
        product.setProductType(type);
        try {
            pm.update(product);
        } catch (ItemNotFoundException ex) {
            Logger.getLogger(CategoryList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setCategory(Category category) {
        this.category = category;
        System.err.println(category);
    }

    public ProductType getType() {
        return type;
    }

    public void setType(ProductType type) {
        this.type = type;
        System.err.println(type);
    }

    @PostConstruct
    public void reset() {
        categories = cm.getAllCategory();
        if (category == null) {
            types = cm.getAllProductType();
        } else {
            try {
                types = cm.getAllProductType(category.getId());
            } catch (ItemNotFoundException ex) {
                Logger.getLogger(CategoryList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public CategoryList() {
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public List<ProductType> getTypes() {
        return types;
    }

    public void setTypes(List<ProductType> types) {
        this.types = types;
    }

    public void categoryChanged() {
        if (category == null) {
            types = cm.getAllProductType();
        } else {
            try {
                types = cm.getAllProductType(category.getId());
            } catch (ItemNotFoundException ex) {
                Logger.getLogger(CategoryList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void typeChaged() {
    }
}
